package com.enjoyor.soa.traffic.rest.efoms.controller;

import com.enjoyor.soa.traffic.frame.annotation.Auth;
import com.enjoyor.soa.traffic.frame.security.bean.dto.UserDto;
import com.enjoyor.soa.traffic.rest.efoms.dubbo.invoke.DubboUbmsServiceInvoke;
import com.enjoyor.soa.traffic.rest.efoms.enums.EnumDicPublicRest;
import com.enjoyor.soa.traffic.util.enums.EnumAppCode;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;
import com.enjoyor.soa.traffic.util.pojo.TreeNode;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

@Auth
@RestController
@RequestMapping({ "/ubmsService" })
@Api("ubms相关rest接口")
@CrossOrigin
public class UbmsDeptInfoController {

    @Autowired
    private DubboUbmsServiceInvoke ubmsInfoServiceInvoke;

    /**
	 * 获取部门信息树；
	 * 
	 * @return fzj
	 */
	@GetMapping(value = { "/getDeptTree" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("获取部门信息树")
	@ApiImplicitParam(name = "deptId", value = "父级部门编号", paramType = "query", dataType = "String")
	public ResultPojo getDeptTree(
			@RequestParam(defaultValue = "", value="deptId", required=false) String deptId,
			HttpServletRequest request) {
		UserDto userDto = (UserDto) request.getAttribute("currentUser");
		String token = (String)request.getAttribute("token");
		String orgId = userDto.getOrgId();
		if(null == deptId || "".equals(deptId)) {
			deptId = orgId;
		}
		List<TreeNode> treeNodeList = ubmsInfoServiceInvoke.getDeptTree(token, deptId);
		return ResultHelper.getResult(treeNodeList);
	}

	/**
	 * 获取区域信息树；
	 * 
	 * @return fzj
	 */
	@GetMapping(value = { "/getRegionTree" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("获取区域信息树")
	@ApiImplicitParam(name = "regionId", value = "父级区域编号", paramType = "query", dataType = "String")
	public ResultPojo getRegionTree(
			@RequestParam(defaultValue = "", value="regionId", required=false) String regionId,
			HttpServletRequest request) {
		ResultPojo resultPojo = ubmsInfoServiceInvoke.getRegionTree(regionId);
		return resultPojo;
	}
	
	/**
	 * 获取设备关联视频点位信息；
	 * 
	 * @return fzj
	 */
	@GetMapping(value = { "/getVideoDeviceRltInfo" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("获取设备关联视频点位信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "devTypeCode", value = "设备类型编号", paramType = "query", required=true, dataType = "String"),
		@ApiImplicitParam(name = "devId", value = "设备编号", paramType = "query", dataType = "String") })
	public ResultPojo getVideoDeviceRltInfo(
			@RequestParam(defaultValue = "", value="devTypeCode", required=true) String devTypeCode,
			@RequestParam(defaultValue = "", value="devId", required=false) String devId,
			HttpServletRequest request) {
		ResultPojo resultPojo = new ResultPojo();
		String type = "";
		if(EnumDicPublicRest.SEMAPHORE.getKey().equals(devTypeCode)) {
			type = "01";
		}else if(EnumDicPublicRest.INSCREEN.getKey().equals(devTypeCode)){
			type = "02";
		}else if(EnumDicPublicRest.FASTENER.getKey().equals(devTypeCode)
				||EnumDicPublicRest.ELEPOLICE.getKey().equals(devTypeCode)){
			type = "03";
		}else if(EnumDicPublicRest.MAGNETISM.getKey().equals(devTypeCode)
				||EnumDicPublicRest.WINDING.getKey().equals(devTypeCode)
				||EnumDicPublicRest.WAVELET.getKey().equals(devTypeCode)){
			type = "04";
		}else {
			resultPojo = ResultHelper.getResult(EnumAppCode.PARAM_ERR, "设备类型编号参数错误！");
		}
		resultPojo = ubmsInfoServiceInvoke.getVideoDeviceRltInfo(type, devId);
		return resultPojo;
	}
	
}
